plotlyplotly scatterplotplotly is both a commercial service and open source product for creating high end interactive visualizations. The plotly package allows you to create plotly interactive graphs from within R.
The ggplotly() function from the plotly package has the ability to translate ggplot2 to plotly. This functionality can be really helpful for quickly adding interactivity to your existing ggplot2 workflow.
Using the Fuel Economy data, we’ll create an interactive graph displaying highway mileage vs. engine displace by car class.
Mousing over a point displays information about that point. Clicking on a legend point, removes that class from the plot. Clicking on it again, returns it. Popup tools on the upper right of the plot allow you to zoom in and out of the image, pan, select, reset axes, and download the image as a png file.
# create plotly graph.
# install.packages("plotly",repos = "http://cran.us.r-project.org")
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
p <- ggplot(mpg, aes(
x = displ,
y = hwy,
color = class
)) +
geom_point(size = 3) +
labs(
x = "Engine displacement",
y = "Highway Mileage",
color = "Car Class"
) +
theme_bw()
ggplotly(p)
By default, the mouse over provides pop-up tooltip with values used to create the plot (dipl, hwy, and class here). However you can customize the tooltip. This involves adding a label = variable to the aes function and to the ggplotly function.
Add ggplotly(p, tooltip = c("label1", "label2", "label3")) to the program to show the plotly graph.
# create plotly graph.
library(ggplot2)
library(plotly)
p <- ggplot(mpg, aes(
x = displ,
y = hwy,
color = class,
label1 = manufacturer,
label2 = model,
label3 = year
)) +
geom_point(size = 3) +
labs(
x = "Engine displacement",
y = "Highway Mileage",
color = "Car Class"
) +
theme_bw()
# add the code here
ggplotly(p, tooltip = c("label1", "label2", "label3"))
The tooltip now displays the car manufacturer, make, and year.
\(\color{red}{\text{In-class exercise}}\)
You can fully customize the tooltip by creating your own label and including it as a variable in the data frame. Then place it in the aesthetic as text and in the ggplot function as a label.
Set tooltip to c("mylabel") to show the graph.
# create plotly graph.
library(ggplot2)
library(plotly)
library(dplyr)
mpg <- mpg %>%
mutate(mylabel = paste(
"This is a", manufacturer, model, "\n",
"released in", year, "."
))
p <- ggplot(mpg, aes(
x = displ,
y = hwy,
color = class,
text = mylabel
)) +
geom_point(size = 3) +
labs(
x = "Engine displacement",
y = "Highway Mileage",
color = "Car Class"
) +
theme_bw()
# write your answer here
ggplotly(p, tooltip = c("mylabel"))
Let’s add fitting curves to the plot.
p <- ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Now let’s draw a bar chart and make it interactive.
To create a non-interactive bar graph using the “mpg” dataset, we use the geom_bar function.
p2 <- ggplot(mpg, aes(x = manufacturer, fill = drv)) +
geom_bar() +
ggtitle("Distribution for Cars based on Drive Type and Manufacturers")
ggplotly(p2)
\(\color{red}{\text{In-class exercise}}\)
Recall what we’ve learned in lab7 and change the position of geom_bar to “dodge”:
# write your answer here
p <- ggplot(mpg, aes(x = manufacturer, fill = drv)) +
geom_bar(position = "dodge") +
ggtitle("Distribution for Cars based on Drive Type and Manufacturers")
ggplotly(p)
Next, we create a bubble graph using the airquality dataset in R. We create a bubble plot using the geom_point(), adding the size as the third dimension.
airQuality_plot <- ggplot(airquality, aes(
x = Day, y = Ozone,
color = as.factor(Month),
text = paste("Month:", Month)
)) +
geom_point(size = airquality$Wind, alpha = 0.6) +
ggtitle("Air Quality in New York by Day") +
labs(x = "Day of the month", y = "Ozone (ppb)", color = "Month") +
scale_x_continuous(breaks = seq(1, 31, 5)) +
scale_size(range = c(1, 10))
However, bubble graphs are even more insightful when they are interactive. The tooltip is to specify the specific information we want to display on the plot.
ggplotly(airQuality_plot, tooltip = c("text", "x", "y"))
The subplot() function provides a flexible interface for merging multiple plotly objects into a single object.
library(plotly)
p1 <- plot_ly(economics, x = ~date, y = ~unemploy) %>%
add_lines(name = "unemploy")
p2 <- plot_ly(economics, x = ~date, y = ~uempmed) %>%
add_lines(name = "uempmed")
subplot(p1, p2)
Although subplot() accepts an arbitrary number of plot objects, passing a list of plots can save typing and redundant code when dealing with a large number of plots. The following figure shows one time series for each variable in the economics dataset and share the x-axis so that zoom/pan events are synchronized across each series:
# Create a vector vars containing the names of all variables in the "economics" dataset except for "date."
vars <- setdiff(names(economics), "date")
# use lapply to iterate over each variable in vars.
# For each variable, it creates a line plot using plot_ly, specifying the x-axis as "date" and the y-axis using a formula constructed from the variable name.
plots <- lapply(vars, function(var) {
plot_ly(economics, x = ~date, y = as.formula(paste0("~", var))) %>%
add_lines(name = var)
})
subplot(plots, nrows = length(plots), shareX = TRUE, titleX = FALSE)
\(\color{red}{\text{In-class exercise}}\)
Specify the nrows of subplot to 3, and heights to c(0.2,0.3,0.5).
# write your answer here
subplot(plots, nrows = 3, shareX = TRUE, titleX = FALSE, heights = c(0.2, 0.3, 0.5))
This is a visualization of bi-variate normal distribution:
# install.packages("mvtnorm",repos = "http://cran.us.r-project.org")
# draw random values from correlated bi-variate normal distribution
s <- matrix(c(1, 0.3, 0.3, 1), nrow = 2)
m <- mvtnorm::rmvnorm(1e5, sigma = s)
x <- m[, 1]
y <- m[, 2]
s <- subplot(
plot_ly(x = x, color = I("red")),
plotly_empty(),
plot_ly(x = x, y = y, color = I("black")) %>%
add_histogram2dcontour(colorscale = "Viridis"),
plot_ly(y = y, color = I("blue")),
nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2), margin = 0,
shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE
)
## No trace type specified:
## Based on info supplied, a 'histogram' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#histogram
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## No trace type specified:
## Based on info supplied, a 'histogram' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#histogram
layout(s, showlegend = FALSE)